package com.coustemview.qianfeng.huaqiangbeistore.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.coustemview.qianfeng.huaqiangbeistore.R;
import com.coustemview.qianfeng.huaqiangbeistore.adapter.mainpage_viewpager_adapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mingma on 2016/8/29.
 */
public class Fragment_mainpage extends Fragment {
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private List<String>titles;
    private List<Fragment> mFragments;
    private FragmentPagerAdapter mPagerAdapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mainpage, null);
        initView(view);
        initData();
        setData();
        return view ;
    }

    private void setData() {
        mViewPager.setAdapter(mPagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
    }

    private void initData() {
      titles=new ArrayList<>();
        mFragments=new ArrayList<>();
        titles.add("海外淘");
        titles.add("特卖会");
        titles.add("手机惠");
        for (int i = 0; i <titles.size() ; i++) {
          if(i==0) {
           mFragments.add(new Fragment_mainpage_oversear());
          }else  if(i==1) {
            mFragments.add(new Fragment_mainpage_specialoffer());
          }else {
           mFragments.add(new Fragment_mainpage_phoneoffer());
          }
        }
        mPagerAdapter=new mainpage_viewpager_adapter(getFragmentManager(),mFragments,titles);
    }

    private void initView(View view) {
       mTabLayout= (TabLayout) view.findViewById(R.id.mainpage_tablayout);
        mViewPager= (ViewPager) view.findViewById(R.id.mainpage_viewpage);
    }
}
